#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/23 15:41
# @Author  : Crissu
# @Site    : 
# @File    : resize.py
# @Software: PyCharm

import cv2, multiprocessing, os

from utils import universal_util
from configs import resize_config

'''
    等比例缩小，补边，补边颜色看齐边界平均值
'''
def changeImageSize(mode=1):
    # 获取当前路径下所有图片名字
    # nameList = universal_util.GetImageSrc(resize_config.FromPath)
    # print(nameList)
    classList = os.listdir(resize_config.FromPath)
    print(classList)

    # 选择模式
    # 0: 需要补边，变成正方形，再resize
    # 1: 需要裁剪长的边跟短边一样长，然后resize
    if mode == 0:
        dealFunc = resize0
    if mode == 1:
        dealFunc = resize1
    universal_util.MakeFloder(resize_config.ToPath)
    pool = multiprocessing.Pool(16)
    pool.map(dealFunc, classList)


'''
    输入边界roi，输出边界RGB平均值
'''
def getRGB(roi):
    # 蓝色
    blueArray = roi[:, :, 0]
    length = len(blueArray)
    blue = 0
    for i in range(length):
        blue += blueArray[i][0]
    blue = int(blue / length)
    # 绿色
    greenArray = roi[:, :, 1]
    green = 0
    for i in range(length):
        green += greenArray[i][0]
    green = int(green / length)
    # 红色
    redArray = roi[:, :, 2]
    red = 0
    for i in range(length):
        red += redArray[i][0]
    red = int(red / length)

    return red, green, blue

'''
    获得边界平均RGB
'''
def getBorderValue(img):
    sp = img.shape
    height = sp[0]
    width = sp[1]
    # 左边
    roi_left = img[:, 0:1]
    red_l, green_l, blue_l = getRGB(roi_left)
    # 右边
    roi_right = img[:, width-1:width]
    red_r, green_r, blue_r = getRGB(roi_right)
    # 上边
    roi_up = img[0:1, :]
    red_u, green_u, blue_u = getRGB(roi_up)
    # 下边
    roi_bottom = img[height-1:height, :]
    red_b, green_b, blue_b = getRGB(roi_bottom)

    red = int((red_b+red_l+red_r+red_u)/4)
    green = int((green_b+green_l+green_r+green_u)/4)
    blue = int((blue_b+blue_l+blue_r+blue_u)/4)
    borderValue = [blue, green, red]
    return borderValue

# 按照指定图像大小调整尺寸
# borderValue -> [155,157,154]
def resize0(img_name):
    # 设置高宽
    height = resize_config.Resize[0]
    width = resize_config.Resize[1]
    # 读取图片
    img = cv2.imread(resize_config.FromPath + img_name)
    # 获取图像四条边的平均像素值
    borderValue = getBorderValue(img)

    top, bottom, left, right = (0, 0, 0, 0)
    # 获取图像尺寸
    h, w, _ = img.shape
    #对于长宽不相等的图片，找到最长的一边
    longest_edge = max(h, w)
    #计算短边需要增加多上像素宽度使其与长边等长
    if h < longest_edge:
        dh = longest_edge - h
        top = dh // 2
        bottom = dh - top
    elif w < longest_edge:
        dw = longest_edge - w
        left = dw // 2
        right = dw - left
    else:
        pass
    # RGB颜色
    # 给图像增加边界，是图片长、宽等长，cv2.BORDER_CONSTANT指定边界颜色由value指定
    constant = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=borderValue)
    # 调整图像大小并保存
    new_img = cv2.resize(constant, (height, width))
    path = resize_config.ToPath
    savePath = path + img_name
    # 保存图片
    cv2.imwrite(savePath, new_img)

    print("-")

# 按照指定图像大小调整尺寸
# 二级目录
def resize1(className):
    path = resize_config.FromPath + className + "/"
    nameList = os.listdir(path)
    for name in nameList:
        rawPath = path + name
        # 设置高宽
        height = resize_config.Resize[0]
        width = resize_config.Resize[1]
        # 读取图片
        img = cv2.imread(rawPath)
        # 获取图像四条边的平均像素值
        borderValue = getBorderValue(img)

        top, bottom, left, right = (0, 0, 0, 0)
        # 获取图像尺寸
        h, w, _ = img.shape
        # 对于长宽不相等的图片，找到最长的一边
        longest_edge = max(h, w)
        # 计算短边需要增加多上像素宽度使其与长边等长
        if h < longest_edge:
            dh = longest_edge - h
            top = dh // 2
            bottom = dh - top
        elif w < longest_edge:
            dw = longest_edge - w
            left = dw // 2
            right = dw - left
        else:
            pass
        # RGB颜色
        # 给图像增加边界，是图片长、宽等长，cv2.BORDER_CONSTANT指定边界颜色由value指定
        constant = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=borderValue)
        # 调整图像大小并保存
        new_img = cv2.resize(constant, (height, width))
        newPath = resize_config.ToPath + className + "/"
        universal_util.MakeFloder(newPath)
        savePath = newPath + name
        # 保存图片
        cv2.imwrite(savePath, new_img)

        print("-")

if __name__ == '__main__':
    imgPath = "./tt/"
    savePath = "./resized/"
    picNames = os.listdir(imgPath)
    for imgName in picNames:
        # 设置高宽
        height = resize_config.Resize[0]
        width = resize_config.Resize[1]
        # 读取图片
        img = cv2.imread(imgPath + imgName)
        # 获取图像四条边的平均像素值
        borderValue = getBorderValue(img)

        top, bottom, left, right = (0, 0, 0, 0)
        # 获取图像尺寸
        h, w, _ = img.shape
        # 对于长宽不相等的图片，找到最长的一边
        longest_edge = max(h, w)
        # 计算短边需要增加多上像素宽度使其与长边等长
        if h < longest_edge:
            dh = longest_edge - h
            top = dh // 2
            bottom = dh - top
        elif w < longest_edge:
            dw = longest_edge - w
            left = dw // 2
            right = dw - left
        else:
            pass
        # RGB颜色
        # 给图像增加边界，是图片长、宽等长，cv2.BORDER_CONSTANT指定边界颜色由value指定
        constant = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=borderValue)
        # 调整图像大小并保存
        new_img = cv2.resize(constant, (height, width))

        # 保存图片
        cv2.imwrite(savePath+imgName, new_img)

        print("-")











